/*
 * HTMLWizardNode.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import de.teamskill.pux.engine.DataElement;
import de.teamskill.pux.engine.InputPort;

/**
 * HTMLWizardNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class HTMLWizardNode extends WizardNode {

	/**
	 * DEFAULT_HTML_CONTENT
	 */
	private static final String DEFAULT_HTML_CONTENT = "<html><body><b>Welcome</b><br/>"
			+ "Please provide real content via the <i>htmlContent</i> "
			+ "configuration port of this node.</body></html>";

	/**
	 * CONFIG_HTML
	 */
	private static final String CONFIG_HTML = "htmlContent";

	/**
	 * html
	 */
	private InputPort html;

	/**
	 * scrollPane
	 */
	private JScrollPane scrollPane;

	/**
	 * htmlTextPane
	 */
	private JTextPane htmlTextPane;

	/**
	 * HTMLWizardNode Constructor
	 * 
	 */
	public HTMLWizardNode() {
		this("HTML-WIZARD" + nr++, false);
	}

	/**
	 * HTMLWizardNode Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public HTMLWizardNode(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#actCancel(java.awt.event.ActionEvent)
	 */
	protected void actCancel(ActionEvent e) {
		getFrame().setVisible(false);
		if (!isContinuous()) {
			getFrame().dispose();
		}
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#actOK(java.awt.event.ActionEvent)
	 */
	protected void actOK(ActionEvent e) {
		getFrame().setVisible(false);
		if (!isContinuous()) {
			getFrame().dispose();
		}
		DataElement de = getInputData().fetchBuffer();
		if (de != null) {
			getOutputData().pushBuffer(de);
		} else {
			getOutputData().pushData(new Long(System.currentTimeMillis()));
		}
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#createPorts()
	 */
	protected void createPorts() {
		super.createPorts();
		html = addConfigurationPort(CONFIG_HTML, String.class, DEFAULT_HTML_CONTENT);
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#createContentPanel()
	 */
	protected Component createContentPanel() {
		JPanel p = new JPanel(new BorderLayout());
		htmlTextPane = new JTextPane();
		htmlTextPane.setEditable(false);
		htmlTextPane.setContentType("text/html");
		String htmlContent = (String) html.fetchData();
		htmlTextPane.setText(htmlContent);
		scrollPane = new JScrollPane(htmlTextPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		p.add(scrollPane, BorderLayout.CENTER);
		return p;
	}

}
